﻿using System;
using System.ComponentModel;
using System.Text;
using System.Web;

public partial class UploadControl : System.Web.UI.UserControl
{
    public UploadControl()
    {
        _MaxFileCount = 5;
        _MaxFileMessage = "Max. File Count";
        
    }

    #region Properties and Fields
    private int _MaxFileCount;

    private string _MaxFileMessage;

    [BrowsableAttribute(true)]
    [DescriptionAttribute("Gets and sets Max Count")]
    [DefaultValueAttribute(5)]
    [Category("Appearance")]
    public int MaxFileCount
    {
        get { return _MaxFileCount; }
        set
        {
            _MaxFileCount = value;
            lblCurrentStatusFileUpload.Text = string.Format("1 of {0}", _MaxFileCount);
        }
    }

    [BrowsableAttribute(true)]
    [DescriptionAttribute("Gets and sets Max File Message")]
    [DefaultValueAttribute("Max. File Count")]
    [Category("Appearance")]
    public string MaxFileMessage
    {
        get { return _MaxFileMessage; }
        set { _MaxFileMessage = value; }
    }


    #endregion
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Page.ClientScript.RegisterClientScriptBlock(GetType(),"UploadScript",GetJavaScript());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
     //   lblMaxFileCount.Text = string.Format("{0} : {1}", MaxFileMessage, MaxFileCount);
        lblCurrentStatusFileUpload.Text = string.Format("1 of {0}", _MaxFileCount);
    }

    public string GetJavaScript()
    {
        StringBuilder JavaScript = new StringBuilder();
        JavaScript.Append("<script language=\"Javascript\" >");
        JavaScript.Append("var x = false;");
        JavaScript.Append("var maxfilecount = ");
        JavaScript.Append(MaxFileCount);
        JavaScript.Append(";");
        JavaScript.Append("var total = 1;");
        JavaScript.Append("function addFileUpload()");
        JavaScript.Append("{");
        JavaScript.Append("if (!document.getElementById || !document.createElement)");
        JavaScript.Append("return;");
        JavaScript.Append("var uploadarea = document.getElementById(\"UploadArea\");");
        JavaScript.Append("if (!uploadarea)");
        JavaScript.Append("return;");
        JavaScript.Append("if (!addFileUpload.lastAssignedId)");
        JavaScript.Append("addFileUpload.lastAssignedId = 100;");
        JavaScript.Append("var newdiv = document.createElement('div');");
        JavaScript.Append("newdiv.innerHTML = '<input id=\"FileFile'+addFileUpload.lastAssignedId+'\" name=\"FileField\"'+addFileUpload.lastAssignedId+' size =60 type=\"file\" /> <a href=\\'#\\' onclick=\\'removeElement('+addFileUpload.lastAssignedId+')\\'>Delete</a>';");
        JavaScript.Append("newdiv.setAttribute('id',\"uploaddiv\"+addFileUpload.lastAssignedId);");
        JavaScript.Append("uploadarea.appendChild(newdiv);");
        JavaScript.Append("addFileUpload.lastAssignedId ++;");
        JavaScript.Append("total++;");
        JavaScript.Append("var messagep = document.getElementById(\"fileinnermessage\");");
        JavaScript.Append("messagep.innerHTML = total+' of '+maxfilecount;");
        JavaScript.Append("if (total == ");
        JavaScript.Append(MaxFileCount);
        JavaScript.Append(")");
        JavaScript.Append("{");
        JavaScript.Append("var btnaddd = document.getElementById(\"btnAddUpload\");");
       // JavaScript.Append("btnaddd.style.visibility = \"hidden\";");
        JavaScript.Append("btnaddd.disabled=\"disabled\";");
        
        JavaScript.Append("}");
        JavaScript.Append("return false;");
        JavaScript.Append("}");
        JavaScript.Append("function removeElement(divNum)");
        JavaScript.Append("{");
        JavaScript.Append("var d = document.getElementById('UploadArea');");
        JavaScript.Append("var olddiv = document.getElementById(\"uploaddiv\"+divNum);");
        JavaScript.Append("d.removeChild(olddiv);");
        JavaScript.Append("total--;");
        JavaScript.Append("var messagep = document.getElementById(\"fileinnermessage\");");
        JavaScript.Append("messagep.innerHTML = total+' of '+maxfilecount;");
        JavaScript.Append("if (total <");
        JavaScript.Append(MaxFileCount);
        JavaScript.Append(")");
        JavaScript.Append("{");
        JavaScript.Append("var btnaddd = document.getElementById(\"btnAddUpload\");");     
        //JavaScript.Append("btnaddd.style.visibility =  \"visible\";");
        JavaScript.Append("btnaddd.disabled=\"\";");
        JavaScript.Append("}");
        JavaScript.Append("}");
        JavaScript.Append("</script>");
        return JavaScript.ToString();
    }

    public HttpFileCollection GetFiles()
    {
        return HttpContext.Current.Request.Files;
    }
}
